import { getBlogConfig } from "@/api/blogconfig";
import nProgress from "nprogress";
import { nextTick } from "react";

import * as TYPE from "../action-type";
import store from "..";

const AppAction = {
  setAliveKeys(key, val) {
    return {
      type: TYPE.APP_SETACTIVEKEYS,
      key,
      val,
    };
  },
  setReloadFlag(flag) {
    store.dispatch({
      type: TYPE.APP_SETRELOADFLAG,
      reloadFlag: flag,
    });
  },
  async reloadPage() {
    nProgress.start();
    this.setReloadFlag(false);

    await new Promise((resolve) => requestAnimationFrame(resolve));
    this.setReloadFlag(true);
    // 滚动到顶部, 模拟刷新

    setTimeout(() => {
      document.documentElement.scrollTo({ left: 0, top: 0 });
      nProgress.done();
    }, 100);
  },
  //加载博客设置
  getBlogInfo() {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await getBlogConfig();
        if (res.code === 0) {
          resolve({
            type: TYPE.APP_GETBLOGINFO,
            blogConfig: res.data,
          });
        } else {
          resolve({
            type: "ERROR",
          });
        }
      } catch (error) {
        resolve({
          type: "ERROR",
        });
      }
    });
  },
};

export default AppAction;
